package com.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beans.Admin;
import com.beans.AdminLog;
import com.dao.AdminDao;
import com.vo.Page;

public class AdminDaoImpl extends UniversalDaoImpl implements AdminDao {

	public Admin login(String name, String pwd,String ip) {
		Admin u=null;
		String sql="from com.beans.Admin where loginName=? and pwd=?";
		Object[] p=new Object[]{name,pwd};
		List l=this.getHibernateTemplate().find(sql, p);
		if(l!=null&&l.size()>0){
			u=(Admin)l.get(0);
			sql="select rcode from j_roller_admin where aid=?";
			Map m=getMap(sql, new Object[] { u.getId() });
			u.setRoller_code((String)m.get("rcode"));
			excuteSQL("insert j_admin_log(adminID,ip) values("+u.getId()+",'"+ip+"')");
		}
		return u;
	}

	public com.vo.Page list(int pageNo, int pageSize, Admin admin) {
		String sql="select a.*,t.code rcode,dbo.getRollerName(t.code) rollerName from j_admin a left join (select aid,code from j_roller_admin ra,j_roller r where ra.rcode=r.code) t on a.id=t.aid where 1=1";
		Map param=new HashMap();
		if(admin.getLoginName()!=null&&!"".equals(admin.getLoginName()))param.put("loginName like ?", "%"+admin.getLoginName()+"%");
		if(admin.getRealName()!=null&&!"".equals(admin.getRealName()))param.put("realName like ?", "%"+admin.getRealName()+"%");
		if(admin.getRoller_code()!=null&&!"".equals(admin.getRoller_code()))param.put("rcode like ?", admin.getRoller_code()+"%");
		return Page(pageNo, pageSize, sql,"id", param);
	}
	public Map getAdmin(int id) {
		String sql="select a.*,t.code rcode,dbo.getRollerName(t.code) rollerName from j_admin a left join (select aid,code from j_roller_admin ra,j_roller r where ra.rcode=r.code) t on a.id=t.aid where id=?";
		return getMap(sql, new Object[]{id});
	}

	public boolean myPwd(int adminID, String oldPwd, String newPwd) {
		String sql="update j_admin set pwd=?,pwd1=pwd,pwd2=pwd1,pwd3=pwd2 where id=? and pwd=?";
		int t=getJdbcTemplate().update(sql,new Object[]{newPwd,adminID,oldPwd});
		if(t==1)
			return true;
		return false;
	}

	public List getUrl() {
		String sql="select url from j_url_do";
		return getJdbcTemplate().queryForList(sql,String.class);
	}

	public boolean findLoginName(String loginName) {
		String sql = "select count(*) a from j_admin where loginName=?";
		Map m = getMap(sql, new Object[] { loginName });
		int a = (Integer) m.get("a");
		if (a == 0)
			return false;
		else
			return true;
	}

	public void insertAdmin(Admin a) {
		insert(a);
		String sql="insert j_roller_admin values("+a.getId()+",'"+a.getRoller_code()+"')";
		excuteSQL(sql);
	}

	public void deleteAdmin(Admin a) {
		String sql="delete j_roller_admin where aid="+a.getId();
		delete(a);
		excuteSQL(sql);
	}

	public void updateAdmin(Admin a) {
		update(a);
		String sql="delete j_roller_admin where aid="+a.getId()+";insert j_roller_admin values("+a.getId()+",'"+a.getRoller_code()+"')";
		excuteSQL(sql);
	}

	public List getMenuList(String roller_code) {
		String sql = "select m.*,rm.rcode checked from j_function_menu m left join j_roller_menu rm on rm.mcode=m.code and rm.rcode='"+roller_code+"' where enabled='Y'  order by m.code";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	public void updateRollerMenu(String rollerCode, String[] functionCode) {
		String sql="delete j_roller_menu where rcode='"+rollerCode+"';\n";
		for(int i=0;i<functionCode.length;i++){
			sql+="insert j_roller_menu(mcode,rcode) values('"+functionCode[i]+"','"+rollerCode+"');\n";
		}
		excuteSQL(sql);
	}
	//角色对应的功能菜单
	public List getMenuByRoller(String rollerCode) {
		String sql = "select m.*,rm.rcode checked from j_function_menu m left join j_roller_menu rm on rm.mcode=m.code and rm.rcode='"+rollerCode+"' where enabled='Y' and rm.rcode is not null  order by m.code";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	public Page getLog(int pageNo,int pageSize,AdminLog log) {
		String sql = "select l.id,l.adminID,l.ip,l.createTime,a.loginName,a.realName from j_admin_log l,j_admin a where a.id=l.adminID";
		Map param=new HashMap();
		if(log.getAdminID()>0)param.put("l.adminID=?", log.getAdminID());
		if(log.getLoginName()!=null&&!"".equals(log.getLoginName()))param.put("a.loginName like ?", "%"+log.getLoginName()+"%");
		return Page(pageNo, pageSize, sql,"createTime", param);
	}

	public String getPath(String url) {
		String sql="select dbo.getNavName((select top 1 code from j_function_menu where  (? like url+'%' or ? like '/'+url+'%') and url<>'')) path";
		Map m=this.getMap(sql, new Object[]{url,url});
		String path="";
		if(m!=null)
			path=(String)m.get("path");
		return path;
	}

	public void getUpdateFunctionURL(String functionCode, String[] url) {
		String sql="delete j_function_url where function_code='"+functionCode+"';\n";
		for(int i=0;i<url.length;i++){
			sql+="insert j_function_url(function_code,url) values('"+functionCode+"','"+url[i]+"');\n";
		}
		excuteSQL(sql);
	}
//角色所属的URL
	public List getURLByRoller(String roller) {
		String sql = "select url from j_roller_menu rm,j_function_url fu where rm.mcode=fu.function_code and rm.rcode=? order by url";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql,new Object[]{roller},String.class);
	}
}